<script setup>
import {User,Lock} from '@element-plus/icons-vue'
import { ref } from 'vue'

import useUserInfoStore from '@/stores/userInfo.js';
const userInfoStore = useUserInfoStore();

const userInfo = ref({...userInfoStore.info})

const info = ref({
    id:userInfo.value.id,
    oldPassword:'',
    newPassword:'',
    surePassword:''
})

const checkRePassword = (rule,value,callback)=>{
    if(value===''){
        callback(new Error('请再次确认密码'))
    }else if(value !== info.value.newPassword){
        callback(new Error('请确保两次输入密码一致'))
    }else{
        callback()
    }
}

const rules = {
    newPassword:[
        {required: true, message: '请输入新密码', trigger: 'blur'},
        {min: 6,max: 18, message: '请输入6-18位密码', trigger: 'blur'}
    ],
    surePassword:[
        {validator:checkRePassword,trigger: 'blur'}
    ]
}

import {resetPasswordService} from '@/api/user.js'
import { ElMessage } from 'element-plus';
const resetPassword = async() => {

    console.log(info)
    let result = await resetPasswordService(info.value)
    ElMessage.success(result.msg?result.msg : '添加成功');

}

</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>修改密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="info" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="旧密码">
                        <el-input :prefix-icon="Lock" v-model="info.oldPassword" placeholder="请输入旧密码"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPassword">
                        <el-input :prefix-icon="Lock" type="password" v-model="info.newPassword" placeholder="请输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="surePassword">
                        <el-input :prefix-icon="Lock" type="password" v-model="info.surePassword" placeholder="请再次确认密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="resetPassword">确认修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>